package com.jixi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jixi.common.result.PageResult;
import com.jixi.common.result.Result;
import com.jixi.mapper.AuditRecordsMapper;
import com.jixi.pojo.entity.AuditRecords;
import com.jixi.service.AuditRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AuditRecordsServiceImpl extends ServiceImpl<AuditRecordsMapper, AuditRecords> implements AuditRecordsService {
	
	@Autowired
	private AuditRecordsMapper auditRecordsMapper;
	
	/**
	 * 审核记录列表
	 *
	 * @param pageNum   页码
	 * @param pageSize  每页数量
	 * @param companyId 餐饮公司id
	 * @param status    审核状态
	 * @return 审核记录列表
	 */
	@Override
	public Result<PageResult> auditRecordsPage(
			Integer pageNum,
			Integer pageSize,
			Long companyId,
			Integer status
	) {
		//分页构造器
		Page<AuditRecords> pageInfo = new Page<>(pageNum, pageSize);
		//条件构造器
		LambdaQueryWrapper<AuditRecords> queryWrapper = new LambdaQueryWrapper<>();
		//只查未删除的
		queryWrapper.eq(AuditRecords::getIsDeleted, 0);
		//添加可能有的过滤条件
		queryWrapper.eq(companyId != null, AuditRecords::getCompanyId, companyId);
		queryWrapper.eq(status != null, AuditRecords::getStatus, status);
		//执行分页查询
		auditRecordsMapper.selectPage(pageInfo, queryWrapper);
		PageResult pageResult = new PageResult(pageInfo.getTotal(), pageInfo.getRecords());
		return Result.success(pageResult);
	}
	
	/**
	 * 通过审核记录id查询审核记录详情
	 *
	 * @param auditId 审核记录id
	 * @return 审核记录详情
	 */
	@Override
	public AuditRecords getAuditRecordsByAuditId(Long auditId) {
		//只查未删除的
		LambdaQueryWrapper<AuditRecords> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(AuditRecords::getIsDeleted, 0);
		queryWrapper.eq(AuditRecords::getAuditId, auditId);
		AuditRecords auditRecords = auditRecordsMapper.selectOne(queryWrapper);
		return auditRecords;
	}
	
	/**
	 * 通过套餐id查询审核记录详情
	 *
	 * @param setmealId 套餐id
	 * @return 审核记录详情
	 */
	@Override
	public AuditRecords getAuditRecordsBySetmealId(Long setmealId) {
		//只查未删除的
		LambdaQueryWrapper<AuditRecords> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(AuditRecords::getIsDeleted, 0);
		queryWrapper.eq(AuditRecords::getAuditId, setmealId);
		AuditRecords auditRecords = auditRecordsMapper.selectOne(queryWrapper);
		return auditRecords;
	}
}
